package com.guch.help.wxapi;

import androidx.appcompat.app.AppCompatActivity;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        switch (baseResp.errCode){
            case BaseResp.ErrCode.ERR_OK:
                //拉起微信成功
//                getAccessToken(((SendAuth.Resp)baseResp).code);

                // 登录成功
                SendAuth.Resp authResp = (SendAuth.Resp) baseResp;
                String code = authResp.code;
                // TODO: 获取到code后，可以通过该code向服务器请求用户信息等操作

                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                // 用户拒绝授权

                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                // 用户取消登陆

                break;
            default:
                break;
        }
    }

    private void getAccessToken(String code) {
        //binding.tvLogin.setText("获取token中");
        String url  = "https://api.weixin.qq.com/sns/oauth2/access_token?"
                +"appid="+"你的appid"
        +"&secret="+"你的appSecret"
        +"&code="+code
                +"&grant_type=authorization_code";

//todo 这个get请求自己封装吧
        /*HttpUtil.getInstance().doGet(url, new HttpUtil.CallBack() {
            @Override
            public void success(String jsong) {
                log("请求token："+jsong);
                Gson gson = new Gson();
//todo 这个bean也自己解析吧
                WXAccessTokenBean bean = gson.fromJson(jsong,WXAccessTokenBean.class);
                if (isEmpty(bean.getErrMsg())){
                    refreshToken(bean);
                }else {
                    loginFail(bean.getErrCode()+bean.getErrMsg());
                }

            }

            @Override
            public void failed(Exception e) {
                loginFail("获取token失败："+e.toString());
            }
        });*/
    }

}
